package edu.pku.sei.modeler.uml.commands.sequence;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.commands.Command;

import edu.pku.sei.modeler.model.structure.MetaDiagram;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.uml.BasicInteractions.DestructionEvent;
import edu.pku.sei.modeler.model.uml.BasicInteractions.ExecutionSpecification;
import edu.pku.sei.modeler.model.uml.BasicInteractions.Interaction;
import edu.pku.sei.modeler.model.uml.BasicInteractions.Lifeline;
import edu.pku.sei.modeler.model.uml.BasicInteractions.StateInvariant;
import edu.pku.sei.modeler.model.uml.Fragments.CombinedFragment;
import edu.pku.sei.modeler.uml.commands.NestedElementCreateCommand;
import edu.pku.sei.modeler.uml.commands.UMLElementCreateCommandFactory;
import edu.pku.sei.modeler.model.uml.Fragments.InteractionUse;
import edu.pku.sei.modeler.model.uml.Kernel.Comment;

public class SequenceDiagramElementCreateCommandFactory extends UMLElementCreateCommandFactory {
	public static SequenceDiagramElementCreateCommandFactory INSTANCE = new SequenceDiagramElementCreateCommandFactory();

	private SequenceDiagramElementCreateCommandFactory() {

	}
	private static final int LIFELINE_FIGURE_Y_LOCATION = 35;
	@Override
	public Command createCommand(MetaNode node, MetaNode hostNode,
			Rectangle constraint) {
		
		if (hostNode.getModelElement() instanceof Interaction 
				&& node.getModelElement() instanceof Lifeline
				) {
			if (constraint != null) {
				constraint.y = LIFELINE_FIGURE_Y_LOCATION;
			}
			return new UMLLifelineCreateCommand(node, hostNode, constraint);
		}
		
		else if (node.getModelElement() instanceof InteractionUse
				&& hostNode.getModelElement() instanceof Interaction) {
			return new UMLInteractionUseCreateCommand(node, hostNode, constraint);
		}
		
		else if (hostNode.getModelElement() instanceof CombinedFragment
				&& node.getModelElement() instanceof CombinedFragment) {
			return new UMLCombinedFragmentCreateCommand(node, hostNode, constraint);
		}
		
		else if (hostNode.getModelElement()instanceof Interaction
				&& node.getModelElement() instanceof CombinedFragment) {
			return new UMLCombinedFragmentCreateCommand(node, hostNode, constraint);
		}
		
		else if (hostNode.getModelElement() instanceof Lifeline
				&& node.getModelElement() instanceof ExecutionSpecification) {
			return new UMLExecutionSpecificationCreateCommand(node, hostNode, constraint);
		}
		
		else if (hostNode.getModelElement() instanceof Lifeline
				&& node.getModelElement() instanceof DestructionEvent) {
			return new UMLStopCreateCommand(node, hostNode, constraint);
		}
		
		else if (hostNode.getModelElement() instanceof Lifeline
				&& node.getModelElement() instanceof StateInvariant) {
			return new UMLStateInvariantCreateCommand(node, hostNode, constraint);
		}
		
		
		else if (hostNode.getModelElement() instanceof Interaction
				&& node.getModelElement() instanceof Comment) {
			return new NestedElementCreateCommand(node, hostNode, constraint);
		}
		
		else return null;
		
	}

	@Override
	public Command createCommand(MetaNode node, MetaDiagram diagram,
			Rectangle constraint) {
		if (node.getModelElement() instanceof Interaction) {
			return super.createCommand(node, diagram, constraint);
		} else if (node.getModelElement() instanceof Comment) { 
			return super.createCommand(node, diagram, constraint);
		} else {
			return null;
		}
	}

}
